package foolcat.tools;

import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.graphics.Color;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.PowerManager;
import android.text.Html;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.ToggleButton;

public class IBTWordsActivity2 extends Activity {
	private String bookname;
	private int listnum;
	private int speed;
	private WordList wl = new WordList();
	private ArrayList<String> curList;
	private int index = 0;
	private int maxList = 0;
    
	Timer timer = new Timer();  
    private TextView t_eng;
    private TextView t_chn;
    private ToggleButton tb;
    public boolean pause = false;

    private PowerManager.WakeLock wlock;
    MediaPlayer mediaPlayer;
    
    private Handler handler = new Handler()  {  
        @Override  
        public void handleMessage(Message msg) {  
        	
        	String word = curList.get(index);
        	String eng = word.substring(0, word.indexOf("$"));
        	String chn = word.substring(word.indexOf("$")+1);

        	t_eng.setTextSize(70);
        	t_eng.setTextColor(Color.YELLOW);
        	t_chn.setTextSize(25);
        	t_eng.setText(Html.fromHtml(eng));
        	t_chn.setText(Html.fromHtml("[list" + listnum + "]" + chn));
        	Log.d(this.getClass().getName(), "Word=["+eng+"]");
        	wl.playMp3(mediaPlayer, getCacheDir(), eng);

        	index++;
            super.handleMessage(msg);  
        }  
    };  	    
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	    
		setVolumeControlStream(AudioManager.STREAM_MUSIC);  
		setContentView(R.layout.main1);
	    t_eng = (TextView) findViewById(R.id.textView1);  
	    t_chn = (TextView) findViewById(R.id.textView2);  
	    tb = (ToggleButton) findViewById(R.id.toggleButton1);
	    tb.setChecked(true);
	    tb.setOnClickListener(new OnClickListener() {
	        public void onClick(View v) {
	            // Perform action on clicks
	        	if (tb.isChecked()) {
	        		pause = false;
	            } else {
	            	pause = true;
	            }
	        }
	    });
	    
	    Bundle bundle = this.getIntent().getExtras();
	    bookname = bundle.getString(IBTWordsActivity.BOOKNAME);
	    listnum = bundle.getInt(IBTWordsActivity.LISTNUM);
	    speed = bundle.getInt(IBTWordsActivity.SPEED);
	    maxList = wl.getBookListCount(bookname);
	    curList = wl.getWordFromList(bookname, listnum);
	    mediaPlayer = new MediaPlayer();
        mediaPlayer.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);
	    
	    timer.scheduleAtFixedRate(new TimerTask() {  
            @Override  
            public void run() {  
            	if (pause) return;
            	if (wl.playing) {
            		Log.d(this.getClass().getName(), "playing, waiting...");
            		return;
            	}
                Message mesasge = new Message();  
                mesasge.what = index;  
                if (index > curList.size() - 1) {
                	listnum++;
                	if (listnum > maxList) {
                		listnum = 1;
                	}
            	    curList = wl.getWordFromList(bookname, listnum);
            	    index = 0;

                }
                handler.sendMessage(mesasge);  
            }  
        }, 0, speed);  
	    PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
        wlock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "IBTWordsActivity2");
        
	}
    @Override  
    protected void onDestroy()  
    {  
        // TODO Auto-generated method stub
    	Log.d(this.getClass().getName(), "onDestroy()");
        super.onDestroy();

    }
    @Override  
    protected void onStop()  
    {  
        // TODO Auto-generated method stub
    	Log.d(this.getClass().getName(), "onStop()");
        timer.cancel();
        mediaPlayer.release();
        mediaPlayer = null;
        super.onStop();

    } 	

    @Override
    protected void onPause() {
    	Log.d(this.getClass().getName(), "onPause()");
    	if (tb.isChecked()) pause = true;
        wlock.release();
        super.onPause();
    }
    
    @Override
    protected void onResume() {
    	Log.d(this.getClass().getName(), "onResume()");
    	if (tb.isChecked()) pause = false;
        wlock.acquire();
        super.onResume();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
      super.onConfigurationChanged(newConfig);
      /*
        if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
        } else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
        }
      */
    }    
}
